import {Request, Response} from "express";
import {checkParams} from "@src/util/util";
import httpStatusCodes from "@src/declarations/major/HttpStatusCodes";
import Constant from "@src/util/constant";
import regionServer from "@src/services/regionServer";
import regionSql from "@src/sql/regionSql";

const paths = {
  // 区域id
  update: "/region-update/:id",
  // 区域id
  relationLiterature: "/relation-literature/:id",
  // 区域id 关联文章
  relationArticle: "/relation-article/:id",
  // 区域id 关联文献
  detail: "/region-detail/:id",
  // 取消文献关联
  cancelRelationLiterature: "/cancel-relation-literature/:id",
  // 取消文章关联
  cancelRelationArticle: "/cancel-relation-article/:id",
  // 关联文章接口列表，区域id
  relationArticleList: "/relation-article-list/:id",
  // 关联文献列表
  relationLiteratureList: "/relation-literature-list/:id",
  // 区域排名
  ranking: "/region-ranking",
};

// 更新区域图片和视频以及文本内容
const update = async (req: Request, res: Response) => {
  const flag = checkParams(req.body, ["content", "images", "videos"]);
  if (!flag) {
    return res.status(httpStatusCodes.BAD_REQUEST).json({data: Constant.LACK});
  }
  const data = await regionServer.update(req);
  return res.status(httpStatusCodes.OK).json({
    data,
  });
};

// 添加文献关联
const relationLiterature = async (req: Request, res: Response) => {
  const {literature_id} = req.body;
  const {id} = req.params;
  // 是否传递文献id
  if (!literature_id) {
    return res.status(httpStatusCodes.BAD_REQUEST).json({data: Constant.LACK});
  }
  const data: any = await regionServer.relationLiterature(id, literature_id);
  return res.status(data.code).json({
    data,
  });
};

// 添加文章关联
const relationArticle = async (req: Request, res: Response) => {
  const {article_id} = req.body;
  const {id} = req.params;
  // 是否传递文章id
  if (!article_id) {
    return res.status(httpStatusCodes.BAD_REQUEST).json({data: Constant.LACK});
  }
  const data: any = await regionServer.relationArticle(id, article_id);
  return res.status(data.code).json({
    data,
  });
};

// 取消文献关联
const cancelRelationLiterature = async (req: Request, res: Response) => {
  const {id} = req.params;
  // 是否传递文献id
  const data: any = await regionServer.cancelRelationLiterature(id);
  return res.status(data.code).json({
    data,
  });
};

// 取消文章关联
const cancelRelationArticle = async (req: Request, res: Response) => {
  const {id} = req.params;
  const data: any = await regionServer.cancelRelationArticle(id);
  return res.status(data.code).json({
    data,
  });
};

// 区域详情
const detail = async (req: Request, res: Response) => {
  const {id} = req.params;
  const data: any = await regionServer.detail(id);
  return res.status(data.code).json({
    data,
  });
};

// 区域关联文章列表
const relationArticleList = async (req: Request, res: Response) => {
  const {id} = req.params;
  const page = req.query.page ? req.query.page : 1;
  const size = req.query.size ? req.query.size : 15;
  const filters = req.query.filters ? req.query.filters : {};
  const data: any = await regionServer.relationArticleList(Number(page), Number(size), filters, id);
  return res.status(data.code).json({data});
};

// 区域关联文献列表
const relationLiteratureList = async (req: Request, res: Response) => {
  const {id} = req.params;
  const page = req.query.page ? req.query.page : 1;
  const size = req.query.size ? req.query.size : 15;
  const filters = req.query.filters ? req.query.filters : {};
  const data: any = await regionServer.relationLiteratureList(Number(page), Number(size), filters, id);
  return res.status(data.code).json({data});
};

// 区域排名
const ranking = async (req: Request, res: Response) => {
  const data = await regionSql.ranking();
  return res.status(data.code).json({
    data,
  });
};

// 省略
export default {
  paths,
  update,
  relationLiterature,
  relationArticle,
  detail,
  cancelRelationLiterature,
  cancelRelationArticle,
  relationArticleList,
  relationLiteratureList,
  ranking,
};